<template>
  <div>
    <!-- TODO:测试已通过 样式未调试 -->
    <el-form :inline="true" :model="cform" class="demo-form-inline">
      <el-form-item label="车间名字">
        <el-input v-model="cform.name" placeholder="车间名字"></el-input>
      </el-form-item>
      <el-form-item label="车间路电">
        <el-input v-model="cform.phone" placeholder="车间路电"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{btnMessage}}</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="id"></el-table-column>
      <el-table-column prop="name" label="车间名字"></el-table-column>
      <el-table-column prop="phone" label="车间路电"></el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="edit(scope.row)" type="text" size="small">编辑</el-button>
          <el-button @click="deleteUser(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination @current-change="handleCurrentChange" :current-page.sync="page" background
                     :hide-on-single-page="true" :page-size="10" layout="total, prev, pager, next" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "CheJian",
  data() {
    return {
      cform: {
        id:'0',
        name: '',
        phone: ''
      },
      tableData:[],
      page:1,
      total:0,
      type:'add',
      btnMessage:'添加'
    }
  },
  created() {
    this.init(1)
  },
  methods: {
    async onSubmit() {
      let formData = new FormData();
      for(const key in this.cform){
        formData.append(key,this.cform[key]);
      }
      const that = this;
      let url = ''
      if(that.type == 'add'){
        url = 'api/user/addChe'
      }
      if(that.type == 'update'){
        url = 'api/user/updateChe'
      }
      let result = await this.$http.post(url, formData)
      result = result.data
      if(result.code == 200){
        that.$message.success(result.message)
        await that.init(that.page)
        that.cform.name = ''
        that.cform.phone = ''
        that.type = 'add'
        that.btnMessage = '添加'
      }else{
        that.$message.error(result.message)
      }
    },
    async init(page){
      const url = 'api/user/listChePage?limit=10&page='+page
      const that = this;
      let result = await this.$http.get(url)
      that.total = result.data.data.length
      that.tableData = result.data.data.list
    },
    handleClick(row){
      console.log(row);
    },
    handleCurrentChange(val){
      this.init(val)
    },
    edit(row){
      this.cform.id = row.id
      this.cform.name = row.name
      this.cform.phone = row.phone
      this.type = 'update'
      this.btnMessage = '修改'
    },
    async deleteUser(row) {
      const url = 'api/user/deleteChe?id='+row.id
      const that = this;
      let page = that.page
      let result = await this.$http.get(url)
      await that.init(page)
      that.$message.success(result.data.message)
    }
  }
}
</script>

<style lang="less" scoped>

</style>
